package com.icinfo.cloud.provider.punish.config.vo;

import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.common.client.vo.PunishListPageVo;
import com.icinfo.cloud.provider.punish.config.enums.DiscretionTypeEnum;
import com.icinfo.cloud.provider.punish.config.model.ConfigOrgRegulatoryDiscretion;
import com.icinfo.framework.tools.utils.StringUtils;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;

/**
 * 自由裁量分页查询对象
 *
 * @author Junfeng.Chen
 * @date 2024/1/31 8:36
 **/
@Data
@NoArgsConstructor
public class ConfigOrgRegulatoryDiscretionPageVo {
    /**
     * 事项名称
     */
    private String regulatoryName;

    /**
     * 领域编码
     */
    private String territoryCode;

    /**
     * 领域名称
     */
    private String territoryName;

    /**
     * 事项代码
     */
    private String regulatoryCode;

    /**
     * 事项唯一id
     */
    private String catalogId;

    /**
     * 是否已经市级裁量配置
     */
    private Boolean alreadyCityConfig;

    /**
     * 启用裁量标准
     */
    private DiscretionTypeEnum discretionType;

    /**
     * 启用裁量标准名
     */
    private String discretionTypeName;

    /**
     * 修改时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm", timezone = "GMT+8")
    private Date updateTime;

    /**
     * 修改用户名称
     */
    private String updateUser;

    /**
     * 主子项类型（1-主项；2-子项）
     */
    private String mainSonType;

    /**
     * 处罚方式集合（1:适用简易程序:2:适用普通程序）
     */
    private List<String> punishWayList;

    /**
     * 处罚方式（1:适用简易程序:2:适用普通程序）
     */
    private String punishWay;

    /**
     * 当前事项主项名称 如果当前事项类型mainSonType为子项2 则存在
     */
    private String mainRegulatoryName;

    /**
     * 标签集合（1:适用简易程序:2:适用普通程序;3:个人情形;4：单位情形）
     */
    private List<String> tags;

    /**
     * 获取启用裁量标准名
     *
     * @return 裁量标准名
     */
    public String getDiscretionTypeName() {
        if (Objects.nonNull(discretionTypeName)) {
            return discretionTypeName;
        }
        if (Objects.isNull(discretionType)) {
            return null;
        }
        return discretionType.getDesc();
    }

    /**
     * 构建分页对象
     *
     * @param vo            检查事项分页对象
     * @param discretionMap 自由裁量配置map key-> catalogId value->配置对象
     * @param ruleMap       key->catalogId value->该部门事项对市级裁量&区级裁量的数量对象
     */
    public ConfigOrgRegulatoryDiscretionPageVo(PunishListPageVo vo, Map<String, ConfigOrgRegulatoryDiscretion> discretionMap,
                                               Map<String, ConfigOrgRegulatoryDiscretionRuleCountVo> ruleMap) {
        this.regulatoryName = vo.getListName();
        this.territoryCode = vo.getTerritoryCode();
        this.territoryName = vo.getTerritoryName();
        this.regulatoryCode = vo.getListCode();
        this.catalogId = vo.getCatalogId();
        this.discretionType = DiscretionTypeEnum.NONE;
        this.mainSonType = vo.getMainSonType();
        this.punishWayList = vo.getApplyPunishWayList();
        this.punishWay = vo.getApplyPunishWay();
        if (StringUtils.isNotEmpty(vo.getMainRegulatoryName())) {
            this.mainRegulatoryName = vo.getMainRegulatoryName();
        }
        if (CollUtil.isEmpty(vo.getApplyPunishWayList())) {
            //兼容数据，检查若返回的数据适用范围为空，则默认为都适用（1:适用简易程序:2:适用普通程序）
            this.punishWayList = Arrays.asList(Constant.STRING_1, Constant.STRING_2);
            this.punishWay = CollUtil.join(punishWayList, ",");
        }
        this.discretionTypeName = DiscretionTypeEnum.NONE.getDesc();
        this.alreadyCityConfig = false;
        LinkedList<String> tagsList = new LinkedList<>();
        //处理 是否已配置 和 标签
        handleAlreadyCityConfigAndTags(vo, ruleMap, tagsList);
        this.tags = tagsList;

        ConfigOrgRegulatoryDiscretion configOrgRegulatoryDiscretion = discretionMap.get(vo.getCatalogId());
        if (Objects.isNull(configOrgRegulatoryDiscretion)) {
            return;
        }
        this.discretionType = DiscretionTypeEnum.getByCode(configOrgRegulatoryDiscretion.getDiscretionType());
        this.discretionTypeName = this.discretionType.getDesc();
        this.updateTime = configOrgRegulatoryDiscretion.getUpdateTime();
        this.updateUser = configOrgRegulatoryDiscretion.getUpdateUser();
    }

    /**
     * 处理 是否违法情形已配置 和 适用程序和违法情形的标签
     *
     * @param vo       检查事项分页对象
     * @param ruleMap  key->catalogId value->该部门事项对市级裁量&区级裁量的数量对象
     * @param tagsList 适用程序 和 违法情形 标签集合
     */
    private void handleAlreadyCityConfigAndTags(PunishListPageVo vo, Map<String, ConfigOrgRegulatoryDiscretionRuleCountVo> ruleMap, LinkedList<String> tagsList) {
        ConfigOrgRegulatoryDiscretionRuleCountVo countVo = ruleMap.get(vo.getCatalogId());
        if (Objects.isNull(countVo)) {
            return;
        }
        this.alreadyCityConfig = countVo.getCityRuleCount() > 0;
        if (countVo.getSimpleCount() > 0) {
            tagsList.add(Constant.STRING_1);
        }
        if (countVo.getNormalCount() > 0) {
            tagsList.add(Constant.STRING_2);
        }
        if (countVo.getPersonRuleCount() > 0) {
            tagsList.add(Constant.STRING_3);
        }
        if (countVo.getOrgRuleCount() > 0) {
            tagsList.add(Constant.STRING_4);
        }
    }
}
